package net.xdclass.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;
import net.xdclass.util.ITextPdfUtil;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RequestMapping("test")
@RestController
public class TestController {

    /**
     * 下载通过ITestPdfUtil生成的pdf文件
     */
    @RequestMapping("download")
    public void download(HttpServletResponse response) {
        String filePath1 = "D:\\home\\test\\test1.pdf";
        String filePath2 = "D:\\home\\test\\test2.pdf";
        String filePath = "D:\\home\\test";

        try {
            // 生成pdf文件
            ITextPdfUtil.generatePdfFile(filePath1);
            ITextPdfUtil.generatePdfFile(filePath2);

            // 将文件打包成压缩包
            File zipFile = ZipUtil.zip(filePath);

            // 设置响应头
            response.setContentType("application/zip");
            response.setHeader("Content-Disposition", "attachment; filename=output.zip");

            // 将压缩包写入响应
            FileUtil.writeToStream(FileUtil.file(zipFile), response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            response.setStatus(500);
        }
    }

}
